package com.zhala.web.pasystem;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.concurrent.ConcurrentHashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;


@Service
public class AlarmService {

    private final ConcurrentHashMap<String, Long> lastProcessedTimeMap = new ConcurrentHashMap<>();
    private final RestTemplate restTemplate;

    @Autowired
    public AlarmService(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    /**
     * 创建实时报警任务
     * @param taskType
     * @return
     */
    public void addAlarmTask(String taskType) {
        // 检查是否在3秒内已经处理过相同的taskType
        long currentTime = System.currentTimeMillis();
        Long lastProcessedTime = lastProcessedTimeMap.get(taskType);
        if (lastProcessedTime != null && (currentTime - lastProcessedTime) < TimeUnit.SECONDS.toMillis(3)) {
            System.out.println("任务已处理，无需重复处理。");
            return;
        }
        // 更新时间戳
        lastProcessedTimeMap.put(taskType, currentTime);

        String thirdPartyUrl = "http://192.168.1.200:12308/pasystemsdk/HttpControlTo";
        Map<String, Object> jsonMap = new LinkedHashMap<>();
        jsonMap.put("cmdtype", "createrealtimetask");
        long timestamp = System.currentTimeMillis();
        jsonMap.put("cmdsqu", timestamp);
        UUID uuid = UUID.randomUUID();
        String randomString = uuid.toString();
        jsonMap.put("taskguid", randomString);
        jsonMap.put("taskname", "报警任务");
        jsonMap.put("triggerdevicenolist", "1,2,3,4,5,6,7");
        jsonMap.put("soundtype", 1);
        jsonMap.put("sourceid", 0);
        jsonMap.put("synchronouscontrol", 0);
        jsonMap.put("volumesetup", 100);
        jsonMap.put("taskpriority", 50);
        jsonMap.put("wholesong", 1);
        jsonMap.put("cycleIndex", 0);
        jsonMap.put("cycleNumber", 1);
        jsonMap.put("custom", 0);
        jsonMap.put("taskhour", 0);
        jsonMap.put("taskminute", 0);
        jsonMap.put("tasksecond", 0);
        jsonMap.put("ttsrate", 0);
        jsonMap.put("ttsname", "default");
        jsonMap.put("ttsvolume", "100");
        jsonMap.put("executetaskafter", 1);
        jsonMap.put("speechcontent", "");


        switch (taskType){
            case "PHONE_CALL":
                //打电话
                jsonMap.put("mediaidlist", "59");
                break;
            case "NO_HELMET":
                //安全帽
                jsonMap.put("mediaidlist", "58");
                break;
            case "OTHER_CLOTHES":
                //反光衣
                jsonMap.put("mediaidlist", "60");
                break;
            case "SMOKING":
                //抽烟
                jsonMap.put("mediaidlist", "59");
                break;
            case "AREA_INVADED":
                //区域入侵
                jsonMap.put("mediaidlist", "61");
                break;
            case "FIRE":
                //火警
                jsonMap.put("mediaidlist", "57");
                break;
            case "STRONG_WIND":
                //火警
                jsonMap.put("mediaidlist", "62");
                break;
        }
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(jsonMap, headers);
        restTemplate.postForEntity(thirdPartyUrl, requestEntity, String.class);
        System.out.println("发送成功");
    }

}
